<?php echo $this->Html->css('inicio'); ?>
<?php   
         echo $this->Html->css('jquery-ui');
         //echo $this->Html->css('estilo'); 
         echo $this->Html->script('jquery.blockUI');
         echo $this->Html->script('jquery.validate');
         echo $this->Html->script('jquery-ui.min');
?>

<script type="text/javascript">
  $(document).ready(function() {
        
       $('form').keypress(function(e){    
            if(e == 13){ 
            return false; 
        } 
       }); 
 
    $('input').keypress(function(e){ 
        if(e.which == 13){ 
            return false; 
        } 
    });
        
     $("#fObra").hide()
    $( "#buscarBusqueda" ).autocomplete({
                        source: function( request, response ) {
				$.ajax({
					url:"personas/complete",
					dataType: "json",
					data: {
						term: request.term
					},
                                        type : 'GET',
					success: function( data ) {
						response( $.map( data, function( item ) {
							return {
								value: item.rut,
                                                                label: item.rut
                                                                
							}
						}));
					}
				});
			},
			minLength: 2,
                        select : function(event, ui){
                                    
                                    $.ajax({
                                        url: 'personas/complete',
                                        data:{ term: ui.item.value},
                                        dataType: "json",
                                        type: 'GET',
                                        success : function(respuesta){                         
                                                     $.each(respuesta, function(k,v){
                                                            
                                                        $("#valor_uno").val(v.nombre);
                                                        $("#valor_dos").val(v.paterno)
                                                        $("#valor_tres").val(v.materno); 
                                                        $("#valor_cuatro").val(v.rut)
                                                        $("#valor_cinco").val(v.dv);
                                                 //console.log(k+" -> " +v.codigo + "," +v.nombre + ", " + v.cantidad )
                                                        mostrarFormu()
                                            })       
                                           
                                                
                                         
                                        
                                        }
                                    });
                                }
		});


    }); 
    
    function mostrarFormu(){
        $("#fObra").dialog({
                modal : true,
                buttons: {
                                Salir : function(){
                                    $(this).dialog('close');
                                    $("#buscarBusqueda").val("");
                                }
                              
			}
                });
    }
   
</script>
<?php if(empty($personas)) : ?>


    <fieldset><legend> No hay personas </fieldset> </legend>
    <?php echo $this->Html->image('icon_pers/addp1.png'); ?>
    <?php echo $this->Html->link('Añadir usuarios',(array('action'=>'add'))); ?>

<?php else: ?>
    
    <?php echo $this->Html->image('icon_pers/home.png'); ?>
    <?php echo $this->Html->link('Home',(array('controller'=>'usuarios','action'=>'admin'))); ?>

    
    <fieldset>
          <legend>
              <br>
            Personas
        </legend>
     </fieldset>
    
    <div id="anchoInput2"> 
    <?php 
        echo $this->Form->create('buscar');
        echo $this->Form->input('Busqueda', array('label'=>'Filtrar por rut'));

       
?>
    
    </div>
     

<table>
    <tr>
        <th>Rut</th>
        <th>Nombre</th>
        <th>Apellido Paterno</th>
        <th>Apellido Materno</th>
        <th>Fecha Nacimiento</th>
    </tr>
    
    <?php foreach ($personas as $people): ?>
    <tr>
        <?php echo $this->Html->charset('UTF-8'); ?>
        <td><?php echo $people['Persona']['rut'] . '-' .$people['Persona']['dv']; ?></td>
        <td><?php echo $people['Persona']['nombre']; ?></td>
        <td><?php echo $people['Persona']['paterno']; ?></td>
        <td><?php echo $people['Persona']['materno']; ?></td>
        <td><?php echo $this->Time->timeAgoInWords($people['Persona']['fecha_nacimiento'] ); ?></td>
        
        
        <td>
        <?php echo $this->Html->image('icon_pers/edit.png'); ?>
        <?php echo $this->Html->link('Editar',
                                      array('action'=>'edit',
                                      $people['Persona']['id']),  
                                      array(),
                                      "¿Esta seguro que desea modificar este registro?");
        ?>
        </td>
        <td>
        <?php echo $this->Html->image('icon_BOD/borrar.png'); ?>
        <?php echo $this->Html->link('Eliminar',
                                    array('action'=>'delete',
                                    $people['Persona']['id']),
                                    array(),
                                    "¿Esta seguro que desea eliminar este registro?");
        ?>
        </td>
    </tr>
    <?php endforeach; ?>
</table>
    
    <div id="fObra">
          <?php echo $this->Form->create('formProducto', array('id' => 'formObra', 'name'=>'formObra', 'style' => 'diplay:none', 'title' => 'Obra destino')) ?>
         <?php 
        
             echo $this->Form->input('obra', array('label' => 'nombre', 'style' => 'width: 200px;', 'id' => 'valor_uno', 'class' => 'selObra', 'disabled' => 'disabled'));
         echo $this->Form->input('codigo', array('label' => 'Apellido Paterno', 'style' => 'width: 200px;', 'id' => 'valor_dos', 'class' => 'selObra','disabled' => 'disabled'));
         echo $this->Form->input('cantidad', array('label' => 'Apellido Materno', 'style' => 'width: 200px;', 'id' => 'valor_tres', 'class' => 'selObra','disabled' => 'disabled'));
            echo $this->Form->input('obra.nombre',array('type'=>'list', 'label' => 'Rut', 'name' => 'nombreObra', 'id'=>'valor_cuatro','disabled' => 'disabled')); 
           echo $this->Form->input('obra_id', array('type' => 'text', 'name' => 'dv', 'id'=>'valor_cinco','disabled' => 'disabled'));
           // echo $this->Form->button('Grabar prestamo', array('name'=>'btnGrabarPrestamo', 'id' => 'btnGrabarPrestamo', 'method' => ''));
            
         ?>
    </div>

    <?php echo $this->Html->image('icon_pers/addp1.png'); ?>
    <?php echo $this->Html->link('Añadir usuarios ',(array('action'=>'add'))); ?>
    <br>
    <?php echo $this->Html->image('icon_pers/ver.png'); ?>   
    <?php echo $this->Html->link('Mostrar todos',(array('action'=>'indexcompleto'))); ?>
    <br>
    <?php echo $this->Html->image('icon_pers/home.png'); ?>
    <?php echo $this->Html->link('Home',(array('controller'=>'usuarios','action'=>'admin'))); ?>

<?php endif; ?>